package joc.contract;

import java.lang.annotation.Annotation;

import joc.customize.ContractRetainingState;
import joc.customize.ForBehaviorCallAtBeginningOfBehavior;
import joc.customize.ForBehaviorEvaluateAtEndOfBehavior;

public class ProfileContract implements ContractRetainingState {
	private long startTime;

	@Override
	public Class<? extends Annotation> getAnnotationClass() {
		return Profile.class;
	}

	@ForBehaviorCallAtBeginningOfBehavior
	public void initContract(Profile annotation) {
		startTime = System.nanoTime();
	}

	@ForBehaviorEvaluateAtEndOfBehavior
	public boolean checkContract(Profile annotation) {
		long nanoValue = annotation.value();
		if (!annotation.nano()) {
			nanoValue *= 1000000;
		}
		return System.nanoTime() - startTime <= nanoValue;
	}
}
